package com.wanxi.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wanxi.VO.ContactVO;
import com.wanxi.dao.ContactDao;
import com.wanxi.dto.ResultDTO;
import com.wanxi.entity.ContactEntity;
import com.wanxi.service.ContactService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : 林子淳
 * @create 2023/2/28 19:57
 */
@Service("contactService")
public class ContactServiceImpl implements ContactService {
    @Autowired
    ContactDao contactDao;
    @Autowired
    ContactEntity contactEntity;

    Jedis jedis = new Jedis();
    @Override
    public ResultDTO add(ContactVO contactVO) {
//        ContactEntity contactEntity = new ContactEntity();
        BeanUtils.copyProperties(contactVO,contactEntity);
        int count = contactDao.add(contactEntity);
        jedis.del("contactList");
        return ResultDTO.getResult(count);
    }

    @Override
    public ResultDTO delete(ContactVO contactVO) {
//        ContactEntity contactEntity = new ContactEntity();
        BeanUtils.copyProperties(contactVO,contactEntity);
        int delete = contactDao.delete(contactEntity);
        jedis.del("contactList");
        return ResultDTO.getResult(delete);
    }

    @Override
    public ResultDTO findById(ContactVO contactVO) {
//        ContactEntity contactEntity = new ContactEntity();

        BeanUtils.copyProperties(contactVO,contactEntity);
        ContactEntity byId = contactDao.findById(contactEntity);
        return ResultDTO.getResult(byId);
    }

    @Override
    public ResultDTO findAll(ContactVO contactVO) {
//        ContactEntity contactEntity = new ContactEntity();
        ObjectMapper mapper = new ObjectMapper();
        BeanUtils.copyProperties(contactVO,contactEntity);
        int count = contactDao.count(contactEntity);
        List<ContactEntity> list = new ArrayList<>();
            list = contactDao.findAll(contactEntity);
            try {
                String contactList = mapper.writeValueAsString(list);
                jedis.set("contactList",contactList);
                if (contactEntity.getPage() != null && contactEntity.getPage() == 1 && jedis.exists("contactList")){
                    String jedisContactList = jedis.get("contactList");
                    JavaType javaType = mapper.getTypeFactory().constructParametricType(List.class, ContactEntity.class);
                    try {
                        list = mapper.readValue(jedisContactList, javaType);
                        jedis.del("contactList");
                    } catch (JsonProcessingException e) {
                        e.printStackTrace();
                    }
                }
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }

        return ResultDTO.getResult("", count, list);
    }

    @Override
    public ResultDTO update(ContactVO contactVO) {
//        ContactEntity contactEntity = new ContactEntity();
        BeanUtils.copyProperties(contactVO,contactEntity);
        int update = contactDao.update(contactEntity);
        jedis.del("contactList");
        return ResultDTO.getResult(update);
    }


}
